﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class add_bus : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int  a, z, bus_id;
        string route_input;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = true;
            Panel4.Visible = false;
            Panel5.Visible = false;
            Panel6.Visible = false;
            route_input = DropDownList4.SelectedValue;
            var q = from p in db.Table_Bus
                    where p.Bus_number == route_input
                    select new { p.Bus_number, p.Bus_licence_plate, p.Province, p.Table_Type_Bus.Type_Bus_name, p.Bus_status };
            GridView1.DataSource = q.ToList();
            GridView1.DataBind();
            string around = @"select *
                            from Table_Bus INNER JOIN Province	
                                ON Table_Bus.Province = Province.ProvinceName 
                            where Table_Bus.Bus_number = '" + route_input + "'";
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(around, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            try
            {
                Session["data00"] = Int32.Parse(dt.Rows[0]["Bus_id"].ToString());
                Session["data01"] = dt.Rows[0]["Bus_number"].ToString();
                Session["data02"] = dt.Rows[0]["Bus_licence_plate"].ToString();
                Session["data03"] = dt.Rows[0]["ProvinceId"].ToString();
                Session["data04"] = Int32.Parse(dt.Rows[0]["Type_Bus_id"].ToString());
                Session["data05"] = dt.Rows[0]["Bus_status"].ToString();
            }
            catch (Exception)
            {

            }              
            
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            Select SData = new Select();
            try
            {
                Table_Bus x = new Table_Bus();
                bus_id = SData.GetId_bus();
                string round_bus = DropDownList8.SelectedValue;
                x.Bus_id = bus_id;
                string text_bus ="";
                string text_round = "";
                if (bus_id.ToString().Length ==1)
	            {   
		            text_bus = "00"+bus_id.ToString();
	            }
                else if (bus_id.ToString().Length ==2)
	            {
		            text_bus = "0"+bus_id.ToString();
	            }
                else if (bus_id.ToString().Length ==3)
	            {
		            text_bus = bus_id.ToString();
	            }
                if (round_bus.Length == 1)
                {
                    text_round = "00"+round_bus.ToString();
                }
                else if (round_bus.Length == 2)
                {
                    text_round = "0"+round_bus.ToString();
                }
                else if (round_bus.Length == 3)
                {
                    text_round = round_bus.ToString();
                }
                x.Bus_number = text_round + "-" + text_bus;
                x.Bus_licence_plate = TextBox1.Text;
                x.Type_Bus_id = Int32.Parse(DropDownList5.SelectedValue);
                x.Province = DropDownList3.SelectedItem.ToString();
                x.Bus_status = "0";
                db.Table_Bus.InsertOnSubmit(x);
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มข้อมูลรถแล้ว');", true);
                //Response.Write(@"<script language='javascript'>alert('เพิ่มข้อมูลรถแล้ว');</script>");
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                Panel6.Visible = false;
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลรถได้');", true);
               //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มข้อมูลรถได้');</script>");
                throw;
            }            
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false;
            Panel5.Visible = false;
            Panel6.Visible = false;
        }
                
        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                Label8.Text = Session["data01"].ToString();
                TextBox3.Text = Session["data02"].ToString();
                DropDownList6.SelectedValue = Session["data04"].ToString();
                DropDownList7.SelectedValue = Session["data03"].ToString();
            }
            catch (Exception)
            {

            }
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = true;
            Panel5.Visible = false;
            Panel6.Visible = false;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = true;
            Panel4.Visible = false;
            Panel5.Visible = true;
            Panel6.Visible = false;
            string check = Session["data01"].ToString();
            string drive = @"select [Bus_pro].[dbo].[Table_drive].drive_id ,[Bus_pro].[dbo].[Table_drive].Bus_number ,[Bus_pro].[dbo].[Table_Personnel].Personnel_prefix,[Bus_pro].[dbo].[Table_Personnel].Personnel_name,[Bus_pro].[dbo].[Table_Personnel].Personnel_surname
                            from [Bus_pro].[dbo].[Table_drive]
                            INNER JOIN [Bus_pro].[dbo].[Table_Personnel]
	                            ON [Bus_pro].[dbo].[Table_drive].Personnel_id = [Bus_pro].[dbo].[Table_Personnel].Personnel_id
	                        where [Bus_pro].[dbo].[Table_drive].Bus_number = '"+check+"'";
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(drive, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                Button11.Visible = true;
            }
            else
            {
                Button11.Visible = false;
            }
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            try
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                Panel6.Visible = false;
            }
            catch (Exception)
            {                
                Panel1.Visible = true;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                Panel6.Visible = false;
            }           
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                try
                {
                    string ch_k = e.CommandArgument.ToString();
                    var q = from p in db.Table_Bus
                            where p.Bus_number == ch_k
                            select p;
                    db.Table_Bus.DeleteAllOnSubmit(q);
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ลบข้อมูลรถสร็จสิ้น');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบข้อมูลรถสร็จสิ้น');</script>");
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    Panel4.Visible = false;
                    Panel5.Visible = false;
                    Panel6.Visible = false;
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถลบข้อมูลรถได้');", true);                   
                }
                
            }
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            try
            {
                var q = from p in db.Table_Bus
                        where p.Bus_number == Label8.Text
                        select p;
                q.ToList().ForEach(c => c.Bus_licence_plate = TextBox3.Text);
                q.ToList().ForEach(c => c.Type_Bus_id = Int32.Parse(DropDownList6.SelectedValue));
                q.ToList().ForEach(c => c.Province = DropDownList7.SelectedItem.ToString());
                q.ToList().ForEach(c => c.Bus_status = "0");                
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('แก้ไขข้อมูลรถแล้ว');", true);
                //Response.Write(@"<script language='javascript'>alert('แก้ไขข้อมูลรถแล้ว');</script>");
                Panel1.Visible = false;
                Panel2.Visible = true;
                Panel3.Visible = true;                
                Panel4.Visible = false;
                Panel5.Visible = false;
                Panel6.Visible = false;
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเแก้ไขข้อมูลรถได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเแก้ไขข้อมูลรถได้');</script>");
                throw;
            }           
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = true;
            Panel4.Visible = false;
            Panel5.Visible = false;
            Panel6.Visible = false;
        }

        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                try
                {
                    a = Convert.ToInt32(e.CommandArgument.ToString());
                    var q = from p in db.Table_drives
                            where p.drive_id == a
                            select p;
                    db.Table_drives.DeleteAllOnSubmit(q);
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ลบข้อมูลรถสร็จสิ้น');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบข้อมูลรถสร็จสิ้น');</script>");
                    Panel1.Visible = false;   
                    Panel2.Visible = true;
                    Panel3.Visible = true;
                    Panel4.Visible = false;                
                    Panel5.Visible = false;
                    Panel6.Visible = false;
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถลบข้อมูลรถได้');", true);
                    throw;
                }
                
            }
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false; 
            Panel5.Visible = false;
            Panel6.Visible = true;
            route_input = DropDownList4.SelectedValue;
            var q = from p in db.Table_Personnels
                    where p.Positions_id == 2
                    select p;
            GridView3.DataSource = q.ToList();
            GridView3.DataBind();            
        }

        protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                try
                {
                    Table_drive x = new Table_drive();
                    Select SData = new Select();
                    z = SData.GetId_drive();
                    x.drive_id = z;
                    x.Bus_number = DropDownList4.SelectedValue;
                    x.Personnel_id = Convert.ToInt32(e.CommandArgument.ToString());
                    db.Table_drives.InsertOnSubmit(x);
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มพนักงานขับรถแล้ว');", true);
                    //Response.Write(@"<script language='javascript'>alert('เพิ่มพนักงานขับรถแล้ว');</script>");
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    Panel3.Visible = true;
                    Panel4.Visible = false;
                    Panel5.Visible = false;
                    Panel6.Visible = false;
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มพนักงานขับรถได้');", true);
                    //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มพนักงานขับรถได้');</script>");
                    throw;
                }        
            }
        }
    }
}